package com.screen.screendisplay.service.impl.al;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.screen.screendisplay.entity.al.AlProdoperPrdSit;
import com.screen.screendisplay.mapper.al.AlProdoperPrdSitMapper;
import com.screen.screendisplay.service.al.AlProdoperPrdSitService;
import com.screen.screendisplay.vo.al.AlProdoperPrdSitVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 生产情况表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class AlProdoperPrdSitServiceImpl extends ServiceImpl<AlProdoperPrdSitMapper, AlProdoperPrdSit> implements AlProdoperPrdSitService {

    @Override
    public List<AlProdoperPrdSitVO> getProductionDataByYearAndType(Integer year, Byte productType) {
        // 查询当年数据
        LambdaQueryWrapper<AlProdoperPrdSit> currentYearWrapper = new LambdaQueryWrapper<>();
        currentYearWrapper.eq(AlProdoperPrdSit::getYear, year)
                .eq(AlProdoperPrdSit::getProductType, productType)
                .orderByAsc(AlProdoperPrdSit::getMonth);
        List<AlProdoperPrdSit> currentYearData = this.list(currentYearWrapper);

        // 查询去年同期数据用于计算历史同期生产量
        Integer lastYear = year - 1;
        LambdaQueryWrapper<AlProdoperPrdSit> lastYearWrapper = new LambdaQueryWrapper<>();
        lastYearWrapper.eq(AlProdoperPrdSit::getYear, lastYear)
                .eq(AlProdoperPrdSit::getProductType, productType)
                .orderByAsc(AlProdoperPrdSit::getMonth);
        List<AlProdoperPrdSit> lastYearData = this.list(lastYearWrapper);

        // 将去年数据按月份分组，方便后续查找
        Map<Integer, AlProdoperPrdSit> lastYearDataMap = lastYearData.stream()
                .collect(Collectors.toMap(AlProdoperPrdSit::getMonth, item -> item, (v1, v2) -> v1));

        // 转换为VO
        List<AlProdoperPrdSitVO> voList = new ArrayList<>();
        for (AlProdoperPrdSit entity : currentYearData) {
            AlProdoperPrdSitVO vo = new AlProdoperPrdSitVO();
            BeanUtils.copyProperties(entity, vo);

            // 计算历史同期生产量
            // 如果去年同期有数据，使用去年同期的实际完成量
            AlProdoperPrdSit lastYearSameMonth = lastYearDataMap.get(entity.getMonth());
            if (lastYearSameMonth != null && lastYearSameMonth.getMonthlyActualAmount() != null) {
                vo.setHistoricalProductionAmount(lastYearSameMonth.getMonthlyActualAmount());
            } else {
                // 如果去年没有数据，根据同比率反推：历史同期 = 实际完成量 / (同比率/100)
                if (entity.getMonthlyActualAmount() != null && entity.getYoyRate() != null 
                        && entity.getYoyRate().compareTo(BigDecimal.ZERO) != 0) {
                    BigDecimal historicalAmount = entity.getMonthlyActualAmount()
                            .divide(entity.getYoyRate().divide(new BigDecimal("100"), 4, RoundingMode.HALF_UP), 
                                    2, RoundingMode.HALF_UP);
                    vo.setHistoricalProductionAmount(historicalAmount);
                } else {
                    vo.setHistoricalProductionAmount(null);
                }
            }

            voList.add(vo);
        }

        return voList;
    }

}
